#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    QPixmap pixmap = QPixmap(":/img/bg5.jpg");
    QPixmap pix = pixmap.scaled(this->size());
    QPalette pal;
    pal.setBrush(QPalette::Background,QBrush(pix));
    this->setPalette(pal);
    db = Database::getInstance().getDatabase();
}

Login::~Login()
{
    delete ui;
}

bool Login::get_logstate()
{
    return log_flag;
}

void Login::on_pushButton_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString passwd = ui->lineEdit_passwd->text();
    QSqlQuery query(db);
    QString sql = QString("select * from users where username='%1' and password='%2'")
            .arg(username).arg(passwd);
    if(!query.exec(sql))
    {
        QMessageBox::warning(this,"登录","登录失败"+query.lastError().text());
        return;
    }
    else
    {
        if(query.next())
        {
            log_flag = true;
            QMessageBox::information(this,"登录","登录成功");
            this->close();
        }
        else {
            // 添加登录失败的消息
            QMessageBox::warning(this, "登录", "登录失败: 用户名或密码错误");
        }
    }
}
